package com.scy.controller;

import com.scy.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/task")
public class MyController {

    static List<byte[]> list = new ArrayList<>();


    /**
     *   -Xms150m -Xmx150m -XX:+PrintGCDetails
     *   -Xms150m -Xmx1500m -XX:+PrintGCDetails
     *   最终模拟,终于明白从节点心跳线程为什么会不定时关闭了
     */
    @RequestMapping("/test1")
    @ResponseBody
    public User test1(User user) {
        System.out.println(user.getName()+"-------进入test1了------"+user.getAddr());
        long maxMemory = Runtime.getRuntime().maxMemory();
        long totalMemory = Runtime.getRuntime().totalMemory();


        String tomcat_output_txt;
        String osName = System.getProperty("os.name");
        if (osName.trim().equals("Linux")) {
            tomcat_output_txt = "/root/apache-tomcat-7.0.57/tomcat_output_txt";
        } else {
            tomcat_output_txt = "d:\\tomcat_output_txt";
        }
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(tomcat_output_txt);
            fileOutputStream.write((maxMemory+"").getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        }

        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);

        scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("日志记录线程执行了");
            }
        }, 0, 3, TimeUnit.SECONDS);

        scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("监控线程执行了");
            }
        }, 0, 20, TimeUnit.SECONDS);

        scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            @Override
            public void run() {
                try {
                    byte[] bytes = new byte[1024 * 1024 * 10];
                    System.out.println("心跳线程执行了. " + bytes.length + "--时间:" + sdf.format(new Date()));
                } catch (Exception e) {
                    System.out.println("进入心跳线程异常-----------------");
                    e.printStackTrace();
                }
            }
        }, 0, 5, TimeUnit.SECONDS);



        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 135; i++) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("循环次数"+i);
                    byte[] bytes=new byte[1024 * 1024];
                    list.add(bytes);
                }
            }
        }).start();

        System.out.println("当前线程执行完成了");

        return new User(tomcat_output_txt, "maxMemory:"+maxMemory+"---totalMemory:"+totalMemory);
    }

}
